package algorithm.List.Stack;

import java.util.LinkedList;
import java.util.Queue;

/**
 * stack可以用数组简单地实现
 */
public class MyStack {
    //简单的数组栈，没有考虑动态扩容
    public static class MyStackArray {
        private int[] stack;
        private int size;

        public MyStackArray(int n) {
            this.stack = new int[n];
            this.size = 0;
        }

        public boolean isEmpty() {
            return size == 0;
        }

        public void push(int val) {
            stack[size++] = val;
        }

        public int pop() {
            return stack[--size];
        }

        public int top() {
            return stack[size - 1];
        }

        public int size() {
            return size;
        }
    }

    public static class MyStackUsingQueue {
        public Queue<Integer> stack;

        public MyStackUsingQueue() {
            stack = new LinkedList<Integer>();
        }

        public void push(int x) {
            int size = stack.size();
            stack.offer(x);
            for (int i = 0; i < size; i++) {
                stack.offer(stack.poll());
            }
        }

        public int pop() {

            return stack.poll();
        }

        public int top() {
            return stack.peek();
        }

        public boolean empty() {
            return stack.isEmpty();
        }
    }
}
